378C - Maze - CodeForces Solution


dfs and similar *1600

Please click on ads to support us..

C++ Code:

#include <iostream>
using namespace std;

char maze[500][500];
int vis[500][500];
//Add a wall if there is just one neibour cell
//NO

//Just visit s-k cells. s is the empty cells then. the remaining would be walls. 

int n, m, k, s, cnt = 0;
bool isOut(int i, int j){
    return (i >= n) || (j >= m) || (j < 0) || (i < 0) ? 1: 0;
}

void DFS(int i, int j){
    if(maze[i][j] == '#' || isOut(i, j) || vis[i][j] || cnt == s-k) 
        return;
    vis[i][j] = 1;
    cnt++;
    DFS(i+1, j);
    DFS(i-1, j);
    DFS(i, j+1);
    DFS(i, j-1);
}


int main(){
    cin >> n >> m >> k;
    s = n*m;
    int x = -1, y = -1;
    for (size_t i = 0; i < n; i++)
    {
        for (size_t j = 0; j < m; j++)
        {   
            vis[i][j] = 0;
            cin >> maze[i][j];
            if(maze[i][j] == '#'){
                s--;
            }else{
                if(x == -1 && y == -1)
                    x = i, y = j;
            }
        }
    }
    DFS(x, y);
    for (size_t i = 0; i < n; i++)
    {
        for (size_t j = 0; j < m; j++)
        {
            if(maze[i][j] != '#' && !vis[i][j])
                cout << 'X';
            else   
                cout << maze[i][j];
        }
        cout << "\n";
    }
    
    return 0;
}


Comments

Submit
0 Comments
More Questions

688B - Lovely Palindromes
66B - Petya and Countryside
1557B - Moamen and k-subarrays
540A - Combination Lock
1553C - Penalty
1474E - What Is It
1335B - Construct the String
1004B - Sonya and Exhibition
1397A - Juggling Letters
985C - Liebig's Barrels
115A - Party
746B - Decoding
1424G - Years
1663A - Who Tested
1073B - Vasya and Books
195B - After Training
455A - Boredom
1099A - Snowball
1651D - Nearest Excluded Points
599A - Patrick and Shopping
237A - Free Cash
1615B - And It's Non-Zero
1619E - MEX and Increments
34B - Sale
1436A - Reorder
1363C - Game On Leaves
1373C - Pluses and Minuses
1173B - Nauuo and Chess
318B - Strings of Power
1625A - Ancient Civilization